package mfund.com.filter;

import javax.servlet.*;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  

public class LoginSessionFilter implements Filter{
				
		private String encodeString;

		@Override
		public void destroy(){

		}

		@Override
		public void doFilter(ServletRequest request,
			ServletResponse response,FilterChain chain) 
			throws IOException,ServletException{

			HttpServletRequest req = (HttpServletRequest)request;  
	        HttpServletResponse res = (HttpServletResponse)response; 
	        HttpSession session = req.getSession(true);  
	        String flag = (String)session.getAttribute("flag");    
	        String root = req.getContextPath();  
	        System.out.println("url:" + req.getRequestURI());  
	    	System.out.println("root:" + req.getContextPath()); 
	    	System.out.println("flag:"+flag); 
	        if (!(flag==null||flag.isEmpty())) {          
	            chain.doFilter(request, response);   
	        } else{  
	            res.sendRedirect(root + "/login.html");       
	        }
		}

		@Override
		public void init(FilterConfig filterConfig) throws ServletException{
			encodeString=filterConfig.getInitParameter("encoding");
		}

}